#include <string.h>
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::unique_ptr;
using std::shared_ptr;
//FILE*使用

void test(){
    FILE * fp = fopen("a.txt", "a+");
    const char * pstr = "hello,world";
    fwrite(pstr, 1, strlen(pstr), fp);

    fclose(fp);
}
//使用智能指针
void test2(){
    unique_ptr<FILE> up(fopen("b.txt", "a+"));
    const char * pstr = "hello,world";
    fwrite(pstr, 1, strlen(pstr), up.get());
    fclose(up.get());
}

class FileDeletor{
public:
    void operator()(FILE * fp){
        if(fp){
            fclose(fp);
        }
    }
};
void test3(){
    unique_ptr<FILE, FileDeletor> up(fopen("b.txt", "a+"));
    const char * pstr = "hello,world";
    fwrite(pstr, 1, strlen(pstr), up.get());
}

void test4(){
    FileDeletor fd;
    shared_ptr<FILE> sp(fopen("b.txt", "a+"), fd);
    const char * pstr = "hello,world";
    fwrite(pstr, 1, strlen(pstr), sp.get());
}

int main()
{
    test4();
    return 0;
}

